<?php

/***************************************************************\
* FILE:		dbfunctions.inc
* SECTION:	core - database interface
* FUNCTION:	provide database interfacing functions
* USES:		function parameters
* INCLUDES:	none
* PRODUCES:	none
* LAUNCHES:	none
\***************************************************************/

connect();

function connect() {
	global $CONFIG;
	if (mysql_connect($CONFIG['db_host'],$CONFIG['db_user'],$CONFIG['db_pwd']))
		 { mysql_select_db($CONFIG['db_name']); }
	else 
		 { die("serious database error while connecting: ".mysql_error()); }
}

function db_catch($error, $mysqlerror) {
	add_alert($mysqlerror);
	die($error);
}

function db_do_query($query) {
	$result = mysql_query($query);
	if ($err = mysql_error()) {
		db_catch("<pre>ERROR: ".$err."\n\nON QUERY:\n\n$query</pre>", $err);
		return 0;
	}
	return $result;
}

function db_num_rows($result) {
	return intval(@mysql_num_rows($result));
}

function db_fetch_array($result) {
	return mysql_fetch_array($result, MYSQL_ASSOC);
}

function db_query_select($vars, $tables, $where, $order = "", $limit = "", $group = "", $having = "") {
	$query = "SELECT";
	while ($t = array_pop($tables)) {
		$va = array_pop($vars);
		if ($va)
			$query .= "\n\t";
			if (is_array($va))
				while ($v = array_pop($va))
					$query .= "$t.$v, ";
			else
				$query .= "$va, ";
			if ($t != "DUMMY")
				$from .= "$t, ";
	}
	$query .= "0\nFROM\n\t".substr($from, 0, -2);
	if ($where)
		$query .= "\nWHERE\n\t$where";
	if ($group)
		$query .= "\nGROUP BY\n\t$group";
	if ($having)
		$query .= "\nHAVING\n\t$group";
	if ($order)
		$query .= "\nORDER BY\n\t$order";
	if ($limit)
		$query .= "\nLIMIT $limit";
	// echo "<pre>$query</pre>";
	return $query;
}

function db_query_insert($keys, $values, $table) {
	$query = "INSERT INTO $table";
	$k = array_pop($keys);
	$v = array_pop($values);
	while ($key = array_pop($keys)) {
		$k .= ", $key";
		$v .= ", ".array_pop($values);
	}
	$query .= "\n\t($k) VALUES\n\t($v)";
	return $query;
}

function db_query_insert_multiple($keys, $values, $table, $ignore=0) {
	$query = "INSERT " .($ignore ? "IGNORE " : ""). "INTO $table";
	$k = array_pop($keys);
	while ($key = array_pop($keys)) {
		$k .= ", $key";
	}
	while ($v = array_pop($values)) {
		$vs .= "(" . array_pop($v);
		while ($vv = array_pop($v))
			$vs .= ", $vv";
		$vs .= "),";
	}
	$query .= "\n\t($k) VALUES\n\t".substr($vs, 0, -1);
	return $query;
}

function db_query_update($keys, $values, $table, $where) {
	$query = "UPDATE $table SET";
	$query .= "\n\t".array_pop($keys)." = ".array_pop($values);
	while ($key = array_pop($keys)) {
		$query .= ",\n\t$key = ".array_pop($values);
	}
	$query .= "\nWHERE\n\t$where";
	return $query;
}

function db_query_delete($table, $where) {
	return "DELETE FROM $table WHERE $where";
}

function db_insert_id() {
	return mysql_insert_id();
}

function db_affected_rows() {
	return mysql_affected_rows();
}

function db_sqlize($string) {
	return '"'.addslashes(trim($string)).'"';
}

function db_desqlize($string) {
	return stripslashes($string);
}

function printquery($sql) {
	echo "<pre>$sql</pre>";
}

?>
